import VueRouter from "vue-router";
import Cookies from 'js-cookie'
import store from "@/store";
import api from "@/api";

const routes = [
    {
        path: "/",
        redirect: "/guizhang"
    },
    // 登陆
    {
        path: "/login",
        component: () => import("@/pages/login/index.vue"),
    },
    {
        path: "/user-info",
        component: () => import("@/pages/index/Index.vue"),
        children:[
            {
                path: "",
                meta: {
                    hideMenu: true,
                    needAuth: true
                },
                component: () => import("@/pages/user-info/index.vue"),
            }
        ]
    },
    {
        path: "/register",
        component: () => import("@/pages/register/index.vue"),
    },
    {
        path: "/guizhang",
        component: () => import("@/pages/index/Index.vue"),
        children: [
            {
                path: "",
                meta: {
                    name: "guizhang"
                },
                component: () => import("@/pages/guizhang/index.vue")
            },
            {
                path: "detail",
                meta: {
                    hideMenu: true,
                    name: "guizhang"
                },
                component: () => import("@/pages/guizhang/detail.vue")
            }
        ]
    },
    {
        path: "/xing_zheng_gui_fan_wen_jian",
        component: () => import("@/pages/index/Index.vue"),
        children: [
            {
                path: "",
                meta: {
                    name: "guizhang"
                },
                component: () => import("@/pages/xing_zheng_gui_fan_wen_jian/index.vue")
            },
            {
                path: "detail",
                meta: {
                    hideMenu: true,
                    name: "guizhang"
                },
                component: () => import("@/pages/xing_zheng_gui_fan_wen_jian/detail.vue")
            }
        ]
    },
    {
        path: "/gong_kai_zhi_lan",
        component: () => import("@/pages/index/Index.vue"),
        children: [
            {
                path: "",
                meta: {
                    name: "gong_kai_zhi_lan"
                },
                component: () => import("@/pages/gong_kai_zhi_lan/index.vue")
            }
        ]
    },
    {
        path: "/gong_kai_zhi_du",
        component: () => import("@/pages/index/Index.vue"),
        children: [
            {
                path: "detail",
                meta: {
                    name: "gong_kai_zhi_du"
                },
                component: () => import("@/pages/gong_kai_zhi_du/detail.vue")
            },
            {
                path: "",
                meta: {
                    name: "gong_kai_zhi_du"
                },
                component: () => import("@/pages/gong_kai_zhi_du/index.vue")
            }
        ]
    }, {
        path: "/zhu_dong_gong_kai",
        component: () => import("@/pages/index/Index.vue"),
        children: [
            {
                path: "ji_gou_xin_xi",
                meta: {
                    name: "zhu_dong_gong_kai"
                },
                component: () => import("@/pages/zhu_dong_gong_kai/ji_gou_xin_xi/index/index.vue")
            },
            {
                path: "gui_hua_xin_xi",
                meta: {
                    name: "zhu_dong_gong_kai"
                },
                component: () => import("@/pages/zhu_dong_gong_kai/gui_hua_xin_xi/index.vue")
            },
            {
                path: "tong_ji_xin_xi",
                meta: {
                    name: "zhu_dong_gong_kai"
                },
                component: () => import("@/pages/zhu_dong_gong_kai/tong_ji_xin_xi/index.vue")
            },
            {
                path: "cai_zheng_jue_suan",
                meta: {
                    name: "zhu_dong_gong_kai"
                },
                component: () => import("@/pages/zhu_dong_gong_kai/cai_zheng_jue_suan/index.vue")
            },
            {
                path: "xing_zheng_shou_fei",
                meta: {
                    name: "zhu_dong_gong_kai"
                },
                component: () => import("@/pages/zhu_dong_gong_kai/xing_zheng_shou_fei/index.vue")
            },
            {
                path: "chu_fa",
                meta: {
                    name: "zhu_dong_gong_kai"
                },
                component: () => import("@/pages/zhu_dong_gong_kai/chu_fa/index.vue")
            },
            {
                path: "zhong_da_xiang_mu",
                meta: {
                    name: "zhu_dong_gong_kai"
                },
                component: () => import("@/pages/zhu_dong_gong_kai/zhong_da_xiang_mu/index.vue")
            },

            ,
            {
                path: "detail",
                meta: {
                    hideMenu: true,
                    name: "zhu_dong_gong_kai"
                },
                component: () => import("@/pages/zhu_dong_gong_kai/detail/index.vue")
            },
            {
                path: "search",
                meta: {
                    hideMenu: true,
                    name: "zhu_dong_gong_kai"
                },
                component: () => import("@/pages/zhu_dong_gong_kai/search/index.vue")
            }

        ]
    }, {
        path: "/shen_qing_gong_kai",
        component: () => import("@/pages/index/Index.vue"),
        children: [
            {
                path: "",
                meta: {
                    name: "shen_qing_gong_kai"
                },
                component: () => import("@/pages/shen_qing_gong_kai/index.vue")
            },
            {
                path: "apply",
                meta: {
                    needAuth: true,
                    hideMenu: true,
                    name: "shen_qing_gong_kai"
                },
                component: () => import("@/pages/shen_qing_gong_kai/apply.vue")
            }
        ]
    }
]

// 3. 创建 router 实例，然后传 `routes` 配置
// 你还可以传别的配置参数, 不过先这么简单着吧。
const router = new VueRouter({
    routes // (缩写) 相当于 routes: routes
})
router.beforeEach((to, from, next) => {

    // 检查跳转链接是否需要登陆验证
    if (to.meta.needAuth) {
        if (!Cookies.get("token")) {
            next("/login")
        }
    }
    if (Cookies.get("token")) {
        // 使用token获取用户信息
        api.userInfo().then((res) => {
            console.log('>>>>>>>>>>>>>>>>>用户信息', res)
            store.state.main.userInfo = res
        })
    }
    next()
})
export default router;
